#######################################################
#######################################################
#######################################################
### CREATED BY JONATHAN KING AND JESSICA SCHOENHERR ###
##### REPLICATION DATA FOR "A MATTER OF OPINION?" #####
#######################################################
#######################################################
#######################################################


library(readxl)
library(tidyverse)
library(ggeffects)
library(stargazer)
library(poliscidata)
library(ggplot2)
library(psych)

# pull in the full sample data
biggerData <- read_excel("NewSurvey20221022.xlsx")

# drop the row with the questions
biggerData <- biggerData %>% filter(StartDate != "Start Date")

# drop out the first six responses, which are from the preview
biggerData <- biggerData %>% filter(Status != "Survey Preview")

# drop out the Spam response
biggerData <- biggerData %>% filter(Status != "Spam")

# drop the no consents
biggerData <- biggerData %>% filter(`Q1...18` == "I consent")

# drop out the people who didn't pass the screener logic
biggerData <- biggerData %>% filter(!is.na(Q153))

# convert the duration data to make it work later
biggerData$duration <- as.numeric(biggerData$`Duration (in seconds)`)

# drop the original variable
data <- biggerData %>% select(-`Duration (in seconds)`)

################
### DURATION ###
################

# duration drop
# drop out anyone who took the survey faster than 1/3 of the median time
describe(data$duration)

# median is 550
550/3
# 183

# drop anyone who finished the survey in less than 183 seconds
data <- data %>% filter(duration >= 183)

### write out the complete RIDs for Lucid
completeRIDs <- data %>% select(rid)
#ridsForTime <- biggerData %>% select(rid, duration) %>% filter(duration < 187)

write.csv(completeRIDs, "~/Dropbox/JAS-JMK/Opinion\ Writer\ Paper/Data/Lucid\ Data\ Check/CompleteRIDS20221022.csv")
#write.csv(ridsForTime, "~/Dropbox/JAS-JMK/Opinion\ Writer\ Paper/Data/Lucid\ Data\ Check/RIDsNotMeetingTime20221007.csv")

### EVERYTHING IS GOING TO BE IN A DIFFERENT PLACE BECAUSE KATEY ADDED THE QUESTIONS TO THE FRONT AND WE REARRANGED ###

##########################
### LEGITIMACY BATTERY ###
##########################

# from Gibson, Caldeira, and Spence 2003
# specific support: 
#	job in government (general approval)
#	too liberal/conservative (general satisfaction with policy)
#	plus questions about policies, but we're not using those
# confidence
#	How much confidence do you have in the U.S. Supreme Court?
# 		this is from the GSS
# six questions for overall "loyalty" or legitimacy


# Q2
# How well do you think the U.S. Supreme Court does its main job in government? Would you say it does a great job, a pretty good job, not a very good job, or a poor job?

data$specificQ1DoesMainJob <- ifelse(data$Q2 == "Poor job", 1, 
	ifelse(data$Q2 == "Not a very good job", 2, 
	ifelse(data$Q2 == "Pretty good job", 3, 
	ifelse(data$Q2 == "Great job", 4, NA))))

# Q3
# In general, would you say that the Supreme Court is too liberal, or too conservative, or about just right in its decisions?

data$specificQ2TooLiberal <- ifelse(data$Q3 == "Much too liberal", 1, 
	ifelse(data$Q3 == "Too liberal", 2, 
	ifelse(data$Q3 == "Just right", 3, 
	ifelse(data$Q3 == "Too conservative", 4, 
	ifelse(data$Q3 == "Much too conservative", 5, NA)))))

# Q4
# How much confidence do you have in the U.S. Supreme Court?

data$confidence <- ifelse(data$Q4 == "Hardly any confidence", 1, 
	ifelse(data$Q4 == "Only some confidence", 2, 
	ifelse(data$Q4 == "A great deal of confidence", 3, NA)))

# Q5
# If the U.S. Supreme Court started making a lot of decisions that most people disagree with, it might be better to do away with the Supreme Court altogether.
	# disagree is the supportive answer, so code backward

data$legitQ1DoAway <- ifelse(data$Q5 == "Disagree strongly", 5, 
	ifelse(data$Q5 == "Disagree somewhat", 4, 
	ifelse(data$Q5 == "Neither agree nor disagree", 3, 
	ifelse(data$Q5 == "Agree somewhat", 2, 
	ifelse(data$Q5 == "Agree strongly", 1, NA)))))

# Q6
# The right of the Supreme Court to decide certain types of controversial issues should be reduced.
	# disagree is the supportive answer, so code backward

data$legitQ2Controversy <- ifelse(data$Q6 == "Disagree strongly", 5, 
	ifelse(data$Q6 == "Disagree somewhat", 4, 
	ifelse(data$Q6 == "Neither agree nor disagree", 3, 
	ifelse(data$Q6 == "Agree somewhat", 2, 
	ifelse(data$Q6 == "Agree strongly", 1, NA)))))

# Q7
# The Supreme Court can usually be trusted to make decisions that are right for the country as a whole.
	# agree is the supportive answer

data$legitQ3Trust <- ifelse(data$Q7 == "Disagree strongly", 1, 
	ifelse(data$Q7 == "Disagree somewhat", 2, 
	ifelse(data$Q7 == "Neither agree nor disagree", 3, 
	ifelse(data$Q7 == "Agree somewhat", 4, 
	ifelse(data$Q7 == "Agree strongly", 5, NA)))))

# Q8
# The decisions of the U.S. Supreme Court favor some groups more than others.
	# supportive answer is disagree, so code backward

data$legitQ4FavorGroups <- ifelse(data$Q8 == "Disagree strongly", 5, 
	ifelse(data$Q8 == "Disagree somewhat", 4, 
	ifelse(data$Q8 == "Neither agree nor disagree", 3, 
	ifelse(data$Q8 == "Agree somewhat", 2, 
	ifelse(data$Q8 == "Agree strongly", 1, NA)))))

# Q9
# The U.S. Supreme Court gets too mixed up in politics.
	# supportive answer is disagree, so code backward

data$legitQ5Politics <- ifelse(data$Q9 == "Disagree strongly", 5, 
	ifelse(data$Q9 == "Disagree somewhat", 4, 
	ifelse(data$Q9 == "Neither agree nor disagree", 3, 
	ifelse(data$Q9 == "Agree somewhat", 2, 
	ifelse(data$Q9 == "Agree strongly", 1, NA)))))

# Q10
# The U.S. Supreme Court should have the right to say what the Constitution means, even when the majority of the people disagree with the Court's decision.
	# supportive answer is agree

data$legitQ6Constitution <- ifelse(data$Q10 == "Disagree strongly", 1, 
	ifelse(data$Q10 == "Disagree somewhat", 2, 
	ifelse(data$Q10 == "Neither agree nor disagree", 3, 
	ifelse(data$Q10 == "Agree somewhat", 4, 
	ifelse(data$Q10 == "Agree strongly", 5, NA)))))

# drop out all the columns we don't need anymore
data <- data %>% select(-StartDate, -EndDate, -Status, -IPAddress, -Progress, -Finished, -RecordedDate, -RecipientLastName, -RecipientFirstName, -RecipientEmail, -ExternalReference, -LocationLatitude, -LocationLongitude, -DistributionChannel, -UserLanguage, -Q1, -Q2, -Q3, -Q4, -Q5, -Q6, -Q7, -Q8, -Q9, -Q10, -Q11)

# legitimacy scale is summative
# increased score = higher support for the Court
# six questions = high of 30, low of 5
data$legitScore <- data$legitQ1DoAway + data$legitQ2Controversy + data$legitQ3Trust + data$legitQ4FavorGroups + data$legitQ5Politics + data$legitQ6Constitution

data$legitAverage <- data$legitScore/6

# look at it using the psych package
# average legit score
legit <- data[c("legitQ1DoAway", "legitQ2Controversy", "legitQ3Trust", "legitQ4FavorGroups", "legitQ5Politics", "legitQ6Constitution")]

psych::alpha(legit)

###########################
### KNOWLEDGE QUESTIONS ###
###########################

# Who is the current Chief Justice of the United States?
data$knowsCJ <- ifelse(is.na(data$Q12), NA, 
	ifelse(data$Q12 == "John Roberts", 1, 0))

# keep the data
table(data$Q12)

# Some judges in the U.S. are elected; others are appointed to the bench. Do you happen to know if the justices of the U.S. Supreme Court are elected or appointed to the bench?

data$appointed <- ifelse(is.na(data$Q13), NA, ifelse(data$Q13 == "Appointed", 1, 0))

# Some judges in the U.S. serve for a set number of years; others serve a life term. Do you happen to know whether the justices of the U.S. Supreme Court serve for a set number of years or whether they serve a life term?

data$lifeTerm <- ifelse(is.na(data$Q14), NA, ifelse(data$Q14 == "Life term", 1, 0))

# Do you happen to know who has the last say when there is a conflict over the meaning of the Constitution -- the U.S. Supreme Court, the U.S. Congress, or the President?

data$lastSay <- ifelse(is.na(data$Q15), NA, ifelse(data$Q15 == "U.S. Supreme Court", 1, 0))

# Do you happen to know if the Supreme Court has made decisions on gay marriage?

data$gayMarriage <- ifelse(is.na(data$Q16), NA, ifelse(data$Q16 == "Yes, it has", 1,
 	ifelse(data$Q16 == "No, it has not", -1, 0)))

# Do you happen to know if the Supreme Court has made decisions on the rights of Black Americans?
data$blackRights <- ifelse(is.na(data$Q17), NA, ifelse(data$Q17 == "Yes, it has", 1,
 	ifelse(data$Q17 == "No, it has not", -1, 0)))

# Do you happen to know if the Supreme Court has made decisions on the maximum income tax rate?

data$taxation <- ifelse(is.na(data$Q18), NA, ifelse(data$Q18 == "Yes, it has", -1,
 	ifelse(data$Q18 == "No, it has not", 1, 0)))

# drop out the variables we don't need anymore
data <- data %>% select(-Q13, -Q14, -Q15, -Q16, -Q17, -Q18)

########################
### DEMOGRAPHIC DATA ###
########################

# gender: 1 = Male, 2 = Female
data$female <- ifelse(data$gender == 1, 0,
	ifelse(data$gender == 2, 1, NA))
	
# political_party
data$democrat <- ifelse(is.na(data$political_party), NA,
	ifelse(data$political_party == 1 | data$political_party == 2 | data$political_party == 3 | data$political_party == 6, 1, 0))
	
data$republican <- ifelse(is.na(data$political_party), NA,
	ifelse(data$political_party == 10 | data$political_party == 9 | data$political_party == 8 | data$political_party == 5, 1, 0))
	
data$independent <- ifelse(is.na(data$political_party), NA,
	ifelse(data$political_party == 4 | data$political_party == 7, 1, 0))

# ethnicity
data$white <- ifelse(is.na(data$ethnicity), NA,
	ifelse(data$ethnicity == 1, 1, 0))

data$black <- ifelse(is.na(data$ethnicity), NA, 
	ifelse(data$ethnicity == 2, 1, 0))

data$notWhite <- ifelse(is.na(data$ethnicity), NA,
	ifelse(data$ethnicity != 1, 1, 0))
	
data$asianPI <- ifelse(is.na(data$ethnicity), NA,
	ifelse(data$ethnicity == 4, 1, 
	ifelse(data$ethnicity == 5, 1, 
	ifelse(data$ethnicity == 6, 1,
	ifelse(data$ethnicity == 7, 1,
	ifelse(data$ethnicity == 8, 1,
	ifelse(data$ethnicity == 9, 1,
	ifelse(data$ethnicity == 10, 1,
	ifelse(data$ethnicity == 11, 1,
	ifelse(data$ethnicity == 12, 1,
	ifelse(data$ethnicity == 13, 1,
	ifelse(data$ethnicity == 14, 1, 0))))))))))))
	
data$nativeAmerican <- ifelse(is.na(data$ethnicity), NA,
	ifelse(data$ethnicity == 3, 1, 0))
	
data$otherRace <- ifelse(is.na(data$ethnicity), NA,
	ifelse(data$ethnicity == 15, 1, 0))

data$noRaceAnswer <- ifelse(is.na(data$ethnicity), NA,
	ifelse(data$ethnicity == 16, 1, 0))
	
# hhi
data$income <- ifelse(data$hhi == -3105, NA, data$hhi)
data$income <- as.numeric(data$income)

# education
data$education <- ifelse(data$education == -3105 | data$education == 10 | data$education == 11 | data$education == 12, NA, data$education)
data$education <- as.numeric(data$education)

# age
data$age <- as.numeric(data$age)

# groups
data$demoGroup <- ifelse(data$female == 0 & data$democrat == 1, 1,
	ifelse(data$female == 0 & data$republican == 1, 2,
	ifelse(data$female == 0 & data$independent == 1, 3,
	ifelse(data$female == 1 & data$democrat == 1, 4,
	ifelse(data$female == 1 & data$republican == 1, 5,
	ifelse(data$female == 1 & data$independent == 1, 6, 0))))))
data$demoGroup <- as.factor(data$demoGroup)

data$partisanship <- ifelse(data$democrat == 1, 1,
	ifelse(data$independent == 1, 2,
	ifelse(data$republican == 1, 3, 0)))
	
data <- data %>% filter(!is.na(data$partisanship))

# note: did check the data using Katey's 5-point scale and the partisanship measure checks out

##################
### EXPERIMENT ###
##################

# put everybody in their treatment group

# 1 - Abortion, liberal, male
# 2 - Abortion, conservative, male
# 3 - Abortion, liberal, female
# 4 - Abortion, conservative, female
# 5 - Abortion, control
# 6 - Death penalty, liberal, male
# 7 - Death penalty, conservative, male
# 8 - Death penalty, liberal, female
# 9 - Death penalty, conservative, female
# 10 - Death penalty, control

data$treatmentGroup <- ifelse(!is.na(data$`Q69_First Click`), 6, 
	ifelse(!is.na(data$`Q79_First Click`), 7, 
	ifelse(!is.na(data$`Q89_First Click`), 8, 
	ifelse(!is.na(data$`Q99_First Click`), 9, 
	ifelse(!is.na(data$`Q109_First Click`), 10, 0)))))
	
# drop out the people who never got put in a treatment group (no idea why?)

data <- data %>% filter(treatmentGroup != 0)

data$writerProfile <- ifelse(!is.na(data$Q70), data$Q70, 
	ifelse(!is.na(data$Q80), data$Q80, 
	ifelse(!is.na(data$Q90), data$Q90, 
	ifelse(!is.na(data$Q100), data$Q100, NA))))
	
data$writerProfileCorrect <- ifelse(data$treatmentGroup == 10, NA,
	ifelse(data$treatmentGroup == 6 & data$writerProfile == "Liberal male", 1, 
	ifelse(data$treatmentGroup == 7 & data$writerProfile == "Conservative male", 1,
	ifelse(data$treatmentGroup == 8 & data$writerProfile == "Liberal female", 1,
	ifelse(data$treatmentGroup == 9 & data$writerProfile == "Conservative female", 1, 
	ifelse(data$writerProfile == "Don't remember", 2, 0))))))

data$deathPenaltyFavor <- ifelse(!is.na(data$Q71), data$Q71, 
	ifelse(!is.na(data$Q81), data$Q81, 
	ifelse(!is.na(data$Q91), data$Q91, 
	ifelse(!is.na(data$Q101), data$Q101, 
	ifelse(!is.na(data$Q110), data$Q110, 0)))))
	
data$deathPenaltyFavorRecode <- ifelse(data$deathPenaltyFavor == "Strongly oppose", 1,
	ifelse(data$deathPenaltyFavor == "Oppose", 2, 
	ifelse(data$deathPenaltyFavor == "Favor", 3,
	ifelse(data$deathPenaltyFavor == "Strongly favor", 4,
	ifelse(data$deathPenaltyFavor == 0 & data$treatmentGroup > 5, NA, NA)))))
	
data$deathPenaltySupport <- ifelse(data$deathPenaltyFavorRecode >= 3, 1, 0)

data$scTherm <- ifelse(!is.na(data$Q72_1), data$Q72_1, 
	ifelse(!is.na(data$Q82_1), data$Q82_1, 
	ifelse(!is.na(data$Q92_1), data$Q92_1, 
	ifelse(!is.na(data$Q102_1), data$Q102_1, 
	ifelse(!is.na(data$Q111_1), data$Q111_1, NA)))))
	
# drop out any NAs with the therm
data <- data %>% filter(!is.na(data$scTherm))
data$scTherm <- as.numeric(data$scTherm)

data$decisionTherm <- ifelse(!is.na(data$Q74_1), data$Q74_1, 
	ifelse(!is.na(data$Q84_1), data$Q84_1, 
	ifelse(!is.na(data$Q94_1), data$Q94_1, 
	ifelse(!is.na(data$Q104_1), data$Q104_1, 
	ifelse(!is.na(data$Q113_1), data$Q113_1, NA)))))
	
# drop out the NAs
data <- data %>% filter(!is.na(data$decisionTherm))
data$decisionTherm <- as.numeric(data$decisionTherm)

data$agreeOrDisagreeDecision <- ifelse(!is.na(data$Q76), data$Q76, 
	ifelse(!is.na(data$Q86), data$Q86, 
	ifelse(!is.na(data$Q96), data$Q96, 
	ifelse(!is.na(data$Q106), data$Q106, 
	ifelse(!is.na(data$Q115), data$Q115, NA)))))
	
data$agreeWithDecisionRecode <- ifelse(data$agreeOrDisagreeDecision == "Agree", 1,
	ifelse(data$agreeOrDisagreeDecision == "Disagree", -1, 0))

data$shouldBeDeciding <-	 ifelse(!is.na(data$Q77), data$Q77, 
	ifelse(!is.na(data$Q87), data$Q87, 
	ifelse(!is.na(data$Q97), data$Q97, 
	ifelse(!is.na(data$Q107), data$Q107, 
	ifelse(!is.na(data$Q116), data$Q116, NA)))))

data$shouldBeDecidingRecode <- ifelse(data$shouldBeDeciding == "Yes, should be", 1, 
	ifelse(data$shouldBeDeciding == "No, should not be", -1,
	ifelse(data$shouldBeDeciding == "No opinion", 0, NA)))

data$deathPenalty <- ifelse(data$treatmentGroup > 5, 1, 0)

# drop out all the raw data I just got rid of
data <- data %>% select(-`Q69_First Click`, -`Q69_Last Click`, -`Q69_Page Submit`, -`Q69_Click Count`, -Q70, -Q71, -Q72_1, -`Q73_First Click`, -`Q73_Last Click`, -`Q73_Page Submit`, -`Q73_Click Count`, -Q74_1, -`Q75_First Click`, -`Q75_Last Click`, -`Q75_Page Submit`, -`Q75_Click Count`, -Q76, -Q77, -`Q79_First Click`, -`Q79_Last Click`, -`Q79_Page Submit`, -`Q79_Click Count`, -Q80, -Q81, -Q82_1, -`Q83_First Click`, -`Q83_Last Click`, -`Q83_Page Submit`, -`Q83_Click Count`, -Q84_1, -`Q85_First Click`, -`Q85_Last Click`, -`Q85_Page Submit`, -`Q85_Click Count`, -Q86, -Q87, -`Q89_First Click`, -`Q89_Last Click`, -`Q89_Page Submit`, -`Q89_Click Count`, -Q90, -Q91, -Q92_1, -`Q93_First Click`, -`Q93_Last Click`, -`Q93_Page Submit`, -`Q93_Click Count`, -Q94_1, -`Q95_First Click`, -`Q95_Last Click`, -`Q95_Page Submit`, -`Q95_Click Count`, -Q96, -Q97, -`Q99_First Click`, -`Q99_Last Click`, -`Q99_Page Submit`, -`Q99_Click Count`, -Q100, -Q101, -Q102_1, -`Q103_First Click`, -`Q103_Last Click`, -`Q103_Page Submit`, -`Q103_Click Count`, -Q104_1, -`Q105_First Click`, -`Q105_Last Click`, -`Q105_Page Submit`, -`Q105_Click Count`, -Q106, -Q107, -`Q109_First Click`, -`Q109_Last Click`, -`Q109_Page Submit`, -`Q109_Click Count`, -Q110, -Q111_1, -`Q112_First Click`, -`Q112_Last Click`, -`Q112_Page Submit`, -`Q112_Click Count`, -Q113_1, -`Q114_First Click`, -`Q114_Last Click`, -`Q114_Page Submit`, -`Q114_Click Count`, -Q115, -Q116)

####################
### DEMOGRAPHICS ###
####################

describe(data$age)
wtd.sd(data$age)

describe(data$female)

describe(data$income)
wtd.sd(data$income)

table(data$white)
table(data$black)
table(data$asianPI)
table(data$nativeAmerican)
table(data$otherRace)
table(data$noRaceAnswer)

table(data$democrat)
table(data$republican)
table(data$independent)

describe(data$education)
wtd.sd(data$education)

##########################
### WRITE OUT THE DATA ###
##########################

write.csv(data, "~/Dropbox/JAS-JMK/Opinion\ Writer\ Paper/Data/DeathPenaltyDataPart2-20221022.csv")
